/**
 * 编码解码配置
 */
 import { app, I_encodeDecodeConfig } from "mydog";

 export function 编码解码器(): { "msgEncode": (cmdId: number, msg: any) => Buffer, "msgDecode": (cmdId: number, msg: Buffer) => any } {
    return { "msgEncode": 编码, "msgDecode": 解码 };
}
function 解码(cmdId: number, msgBuf: Buffer): any {
    let msg = 编码器[cmdId].服务端接收?.decode(msgBuf);
    console.log("--->>>", app.routeConfig[cmdId], JSON.stringify(msg));
    return msg;
}
/**
 * 编码
 * @param cmdId 
 * @param data json
 * @returns buffer
 */
function 编码(cmdId: number, data: any): Buffer {
    
    console.log("<<<---cmd:", app.routeConfig[cmdId], JSON.stringify(data));
    
    return 编码器[cmdId].服务端发送?.encode(data).finish() as Buffer;
}

// 编码时直接中转Buffer如排行版数据,一小时编译一次,
let encode_buffer = {
    "encode": function (data: Buffer) {
        return {
            "finish": function () {
                return data;
            }
        }
    }
}
// 解码时直接中转Buffer
let decode_buffer = {
    "decode": function (data: Buffer) {
        return data;
    }
}

interface I_msg_con {
    服务端接收?: { decode: (msg: Buffer) => any };
    服务端发送?: { encode: (msg: any) => { finish: () => Uint8Array } };
}


let 编码器: { [cmd: string]: I_msg_con } = {};
//编码器[cmd.connector_main_ping] = { "服务端接收": cs_msg.c2s_connector_main_ping, "服务端发送": cs_msg.s2c_connector_main_ping };
//编码器[cmd.onHello] = { "服务端发送": cs_msg.s2c_onHello };
